package com.example.express.controller;

import com.example.express.bean.*;
import com.example.express.mvc.ResponseBody;
import com.example.express.service.BaseUserService;
import com.example.express.service.impl.UserServiceImpl;
import com.example.express.util.DateFormatUtil;
import com.example.express.util.JSONUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class UserController {
    private BaseUserService service = new UserServiceImpl();

    @ResponseBody("/user/console.do")
    public String console(HttpServletRequest req, HttpServletResponse response){
        List<Map<String,Integer>> map = service.console();
        Message msg = new Message();
        if (map.size()==0){
            msg.setStatus(-1);
        }else {
            msg.setStatus(0);
        }
        msg.setData(map);
        return JSONUtil.toJSON(msg);
    }

    @ResponseBody("/user/list.do")
    public String list(HttpServletRequest req, HttpServletResponse response){
        int offset = Integer.parseInt(req.getParameter("offset"));
        int pageNumber = Integer.parseInt(req.getParameter("pageNumber"));
        List<User> data = service.findAll(true,offset,pageNumber);
        List<BootStrapTableUser> list = new ArrayList<>();
        for (User user:data){
            String registDate = DateFormatUtil.format(user.getRegistDate());
            String loginTime = user.getLoginTime()==null?"未登录":DateFormatUtil.format(user.getLoginTime());
            BootStrapTableUser tableUser = new BootStrapTableUser(user.getId(), user.getNickname(), user.getPhoneNumber(),
                    user.getIdNumber(), user.getPassword(), registDate,loginTime);
            list.add(tableUser);
        }
        Integer total = service.console().get(0).get("data4_size");
        ResultData<BootStrapTableUser> resultData = new ResultData<>();
        resultData.setTotal(total);
        resultData.setRows(list);
        return JSONUtil.toJSON(resultData);
    }

    @ResponseBody("/user/add.do")
    public String insert(HttpServletRequest req, HttpServletResponse response){
        String nickname = req.getParameter("nickname");
        String phoneNumber = req.getParameter("phoneNumber");
        String idNumber = req.getParameter("idNumber");
        String password = req.getParameter("password");
        User user = new User(nickname,idNumber,phoneNumber,password);
        boolean insert = service.insert(user);
        Message msg = new Message();
        if (insert){
            msg.setStatus(0);
            msg.setResult("快递员录入成功");
        }else {
            msg.setStatus(-1);
            msg.setResult("快递员录入失败");
        }
        return JSONUtil.toJSON(msg);
    }

    @ResponseBody("/user/find.do")
    public String find(HttpServletRequest req, HttpServletResponse response){
        String phoneNumber = req.getParameter("phoneNumber");
        User user = service.findByPhoneNum(phoneNumber);
        Message msg = new Message();
        if (user == null) {
            msg.setStatus(-1);
            msg.setResult("手机号不存在");
        }else {
            msg.setStatus(0);
            msg.setResult("查询成功");
            msg.setData(user);
        }
        return JSONUtil.toJSON(msg);
    }
    @ResponseBody("/user/update.do")
    public String update(HttpServletRequest req, HttpServletResponse response){
        int id = Integer.parseInt(req.getParameter("id"));
        String nickname = req.getParameter("nickname");
        String phoneNumber = req.getParameter("phoneNumber");
        String idNumber = req.getParameter("idNumber");
        String password = req.getParameter("password");
        User user = new User(id,nickname,idNumber,phoneNumber,password);
        boolean update = service.update(id,user);
        Message msg = new Message();
        if (update){
            msg.setStatus(0);
            msg.setResult("更新成功");
        }else {
            msg.setStatus(-1);
            msg.setResult("更新失败");
        }
        return JSONUtil.toJSON(msg);
    }

    @ResponseBody("/user/delete.do")
    public String delete(HttpServletRequest req,HttpServletResponse resp){
        int id = Integer.parseInt(req.getParameter("id"));
        boolean delete = service.delete(id);
        Message msg = new Message();
        if (delete){
            msg.setStatus(0);
            msg.setResult("删除成功");
        }else {
            msg.setStatus(-1);
            msg.setResult("删除失败");
        }
        return JSONUtil.toJSON(msg);
    }
}
